<?php
function nodetree($arr, $checkarr, $pid = 0) {
	$tree = array();
	foreach($arr as $key => $value) {
		if ($value['pid'] == $pid) {
			$data['id'] = intval($value['id']);
			$data['text'] = $value['title'];
			if (in_array($value['id'],$checkarr)) {
				$data['stats']['checked'] = true;
			}

			foreach ($arr as $subvalue) {
				if ($subvalue['pid'] == $value['id']) {
					$data['children'] = nodetree($arr,$checkarr, $value['id']);
					break;
				}
			}
			$tree[] = $data;
			unset($data);
		}
	}
	return $tree;
}

/**
 * kindeditor图片上传到upyun
 */
function uploadPic() {
	//定义允许上传的文件扩展名
	$ext_arr = array(
	'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
	);
	//最大文件大小
	$max_size = 10000000;
	//PHP上传失败
	if (!empty($_FILES['imgFile']['error'])) {
		switch($_FILES['imgFile']['error']){
			case '1':
				$error = '超过php.ini允许的大小。';
				break;
			case '2':
				$error = '超过表单允许的大小。';
				break;
			case '3':
				$error = '图片只有部分被上传。';
				break;
			case '4':
				$error = '请选择图片。';
				break;
			case '6':
				$error = '找不到临时目录。';
				break;
			case '7':
				$error = '写文件到硬盘出错。';
				break;
			case '8':
				$error = 'File upload stopped by extension。';
				break;
			case '999':
			default:
				$error = '未知错误。';
		}
		echo json_encode(array('error' => 1, 'message' => $error));
		exit;
	}
	//有上传文件时
	if (empty($_FILES) === false) {
		//原文件名
		$file_name = $_FILES['imgFile']['name'];
		//服务器上临时文件名
		$tmp_name = $_FILES['imgFile']['tmp_name'];
		//文件大小
		$file_size = $_FILES['imgFile']['size'];
		//检查文件名
		if (!$file_name) {
			echo json_encode(array('error' => 1, 'message' => "请选择文件。"));
			exit;
		}
		//检查是否已上传
		if (@is_uploaded_file($tmp_name) === false) {
			echo json_encode(array('error' => 1, 'message' => "上传失败。"));
			exit;
		}
		//检查文件大小
		if ($file_size > $max_size) {
			echo json_encode(array('error' => 1, 'message' => "上传文件大小超过限制。"));
			exit;
		}
		//获得文件扩展名
		$temp_arr = explode(".", $file_name);
		$file_ext = array_pop($temp_arr);
		$file_ext = trim($file_ext);
		$file_ext = strtolower($file_ext);

		$configfilepath='/apps/admin/Conf/yun.php';
		$isopenyun = service("Xattach") -> isOpenYun($configfilepath);
		if($isopenyun){
			$file_url = service("Xattach") -> uploadUpYun("photo",$configfilepath);
			if($file_url === false){
				echo json_encode(array('error' => 1, 'message' => "上传文件失败。"));
				exit;
			}
		}else{
			echo json_encode(array('error' => 1, 'message' => "云服务未配置。"));
			exit;
		}
		header('Content-type: text/html; charset=UTF-8');
		echo json_encode(array('error' => 0, 'url' => $file_url));
		exit;
	}
}
?>
